package com.parkcar;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import adapter.TimeWheelListAdapter;
import ui.wheel.OnWheelChangedListener;
import ui.wheel.OnWheelScrollListener;
import ui.wheel.WheelView;
import ui.wheel.adapter.ArrayWheelAdapter;
import util.BaseActivity;
import util.Constrants;

/**
 * Created by Administrator on 2016/3/10.
 */
public class OrderTimeActivity extends BaseActivity implements OnWheelChangedListener, OnWheelScrollListener {
    private WheelView dateWheel,hourWheel,minWheel;
    private static List<String> DATES = new ArrayList<String>();
    private static List<String> HOURS = new ArrayList<String>();
    private static List<String> MINS = new ArrayList<>();
    private TextView time_text;
    private  int currentDate=0,currentHour=0,currentMin=0;
    private  String dayTime="",hourTime="",minTime="";
    private int maxTextSize = 18;
    private int minTextSize = 12;
    private TimeWheelListAdapter dateAdapter,hourAdapter,minAdapter;
    @Override
    protected void setUpui(Bundle arg0) {
        setContentView(R.layout.activity_order_time);
        setTitleGone(View.GONE);
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参值
        p.height = (int) (d.getHeight() * 1.0); // 高度设置为屏幕的1.0
        p.width = (int) (d.getWidth() * 1.0); // 宽度设置为屏幕的1.0
        p.alpha = 1.0f; // 设置本身透明度
        p.dimAmount = 0.0f; // 设置黑暗度
        getWindow().setAttributes(p); // 设置生效
        getWindow().setGravity(Gravity.BOTTOM); // 设置靠右对齐
        dateWheel = (WheelView) findViewById(R.id.dateWheel);
        hourWheel = (WheelView) findViewById(R.id.hourWheel);
        minWheel = (WheelView) findViewById(R.id.minWheel);
        time_text = (TextView) findViewById(R.id.time_text);
        initData();
        initDayView();
        initHourView();
        initMinView();
        setTextViewValue();
        hourWheel.addChangingListener(this);
        dateWheel.addChangingListener(this);
        minWheel.addChangingListener(this);
        hourWheel.addScrollingListener(this);
        dateWheel.addScrollingListener(this);
        minWheel.addScrollingListener(this);
    }
    private void initDayView(){
        dateAdapter = new TimeWheelListAdapter(OrderTimeActivity.this,DATES,currentDate,maxTextSize,minTextSize);
        dateWheel.setViewAdapter(dateAdapter);
        dateWheel.setCurrentItem(currentDate);
    }
    private void initHourView(){
        hourAdapter = new TimeWheelListAdapter(OrderTimeActivity.this,HOURS,currentHour,maxTextSize,minTextSize);
        hourWheel.setViewAdapter(hourAdapter);
        hourWheel.setCurrentItem(currentHour);
    }
    private void initMinView(){
        minAdapter = new TimeWheelListAdapter(OrderTimeActivity.this,MINS,currentMin,maxTextSize,minTextSize);
        minWheel.setViewAdapter(minAdapter);
        minWheel.setCurrentItem(currentMin);
    }
    @Override
    protected void updateUI(boolean isLogin) {

    }
    public void toCancle(View view){
        finish();
    }
    public void toSure(View view){
       String odertime = dayTime+" "+hourTime+":"+minTime;
        Intent mintent = new Intent();
        mintent.putExtra("ordertime",odertime);
        setResult(Constrants.SELECTTIME,mintent);
        toast(odertime);
        finish();
    }
    private void initData() {
        getDates();
        for(int i=0;i<24;i++){
            String hour ="";
            if(i<10){
                hour="0"+String.valueOf(i);
            }else{
                hour=String.valueOf(i);
            }
            HOURS.add(hour);
        }
        for(int j=0;j<60;j++){
            String minute ="";
            if(j<10){
                minute="0"+String.valueOf(j);
            }else{
                minute=String.valueOf(j);
            }
            MINS.add(minute);
        }
    }
    private void  setTextViewValue(){
        time_text.setText("预订至：" +dayTime+" "+hourTime+":"+minTime);
    }
    public  void getDates() {
        Date nowDate= new Date();
        dayTime = nowDate.getDay()+"";
        hourTime = nowDate.getHours()+"";
        minTime = nowDate.getMinutes()+"";
        currentHour = nowDate.getHours()-1;
        currentMin = nowDate.getMinutes()-1;
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        DATES.add(format.format(nowDate));
        DATES.add(format.format(calendar.getTime()));
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        DATES.add(format.format(calendar.getTime()));
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        switch (wheel.getId()){
            case R.id.dateWheel:
                currentDate = wheel.getCurrentItem();
                initDayView();
                break;
            case R.id.hourWheel:
                currentHour = wheel.getCurrentItem();
                initHourView();
                break;
            case R.id.minWheel:
                currentMin = wheel.getCurrentItem();
                initMinView();
                break;
        }
        getCurrentDatas();
        setTextViewValue();
    }

    @Override
    public void onScrollingStarted(WheelView wheel) {

    }

    @Override
    public void onScrollingFinished(WheelView wheel) {
        switch (wheel.getId()){
            case R.id.dateWheel:
                currentDate = wheel.getCurrentItem();
                initDayView();
                break;
            case R.id.hourWheel:
                currentHour = wheel.getCurrentItem();
                initHourView();
                break;
            case R.id.minWheel:
                currentMin = wheel.getCurrentItem();
                initMinView();
                break;
        }
        getCurrentDatas();
        setTextViewValue();
    }
    private void getCurrentDatas(){
        dayTime = DATES.get(dateWheel.getCurrentItem());
        hourTime = HOURS.get(hourWheel.getCurrentItem());
        minTime = MINS.get(minWheel.getCurrentItem());
    }
}
